package com.ruoyi.web.controller.kyhs;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.kysh.entity.BorerInf;
import com.ruoyi.system.kysh.service.BorerInfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.system.kysh.entity.DrillInf;
import com.ruoyi.system.kysh.entity.PlaceInf;
import com.ruoyi.system.kysh.service.BorerInfService;
import com.ruoyi.system.kysh.utils.MyTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

@RestController
@RequestMapping("/kyhs/borer")
public class BorerInfController {
    @Autowired
    private BorerInfService borerInfService;
    /**
     * @Param page 当前页
     * @Param pageSize每页数量
     * @return 结果
     */
    @GetMapping("/page")
    public AjaxResult taskPage(Long id,@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize){

        //分页构造器
        Page<BorerInf> pageInfo=new Page<>(page,pageSize);
        //进行分页查询
        LambdaQueryWrapper<BorerInf> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //只查询未删除信息
        //逻辑查询
        lambdaQueryWrapper.eq(BorerInf::getDeleteLogic,"0");
        //根据更新时间和创建时间排序
        lambdaQueryWrapper.orderByDesc(BorerInf::getUpdateTime);
        lambdaQueryWrapper.orderByDesc(BorerInf::getCreateTime);
        //id编号查询
        if(id!=null && !"".equals(id.toString())){
            lambdaQueryWrapper.eq(BorerInf::getBorerId,id);
        }
        borerInfService.page(pageInfo, lambdaQueryWrapper);
        return AjaxResult.success("操作成功",pageInfo);
    }
    /**
     *
     * @param id 钻机编号
     * @return 结果
     */
    @GetMapping("/select-by-id")
    public AjaxResult getTaskById(Long id){
        //如果id为空则提示输入信息
        if(id==null || "".equals(id.toString())){
            return AjaxResult.warn("请输入相应信息");
        }
        LambdaQueryWrapper<BorerInf> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //查询对应id且未删除信息
        lambdaQueryWrapper
                .eq(BorerInf::getBorerId,id)
                .eq(BorerInf::getDeleteLogic,"0");

        BorerInf oneTask = borerInfService.getOne(lambdaQueryWrapper);
        //查询到id为空则返回空信息
        return AjaxResult.success("操作成功",oneTask);
    }
    /**
     *
     * @param borerinf 所删除对象
     * @return 删除是都成功
     */
    @DeleteMapping("/delete-one")
    public AjaxResult deleteOne(@RequestBody BorerInf borerinf){

        LambdaQueryWrapper<BorerInf> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BorerInf::getBorerId, borerinf.getBorerId());
        BorerInf oneTask = borerInfService.getOne(lambdaQueryWrapper);
        //没有搜索到则返回空内容
        if(oneTask.getDeleteLogic().equals("1")){
            return AjaxResult.error("已删除，请勿重复操作");
        }
        oneTask.setDeleteLogic("1");//已删除状态
        //重置删除后的id
        boolean update = borerInfService.updateById(oneTask);
        if(update){
            return AjaxResult.success("操作成功");
        }else {

            return AjaxResult.error("未知错误");
        }
    }

    /**
     *
     * @param borerinf 所新建对象
     * @return 是否成功
     */
    @PostMapping("/save-one")
    public AjaxResult saveOne(@RequestBody BorerInf borerinf){
        //是否已经存在
        LambdaQueryWrapper<BorerInf> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BorerInf::getBorerId, borerinf.getBorerId());
        BorerInf oneTask = borerInfService.getOne(lambdaQueryWrapper);

        borerinf.setCreateTime(MyTimeUtil.nowTime());
        borerinf.setUpdateTime(MyTimeUtil.nowTime());

        //存在则进行更新
        if(oneTask!=null){
            if(oneTask.getDeleteLogic().equals("0")){
                return AjaxResult.error("当前钻机编号已存在");
            }
            borerinf.setDeleteLogic("0");//修改逻辑删除状态

            borerinf.setId(oneTask.getId());

            borerInfService.updateById(borerinf);
            return AjaxResult.success("新建成功");
        }

        //不存在则进行存储
        borerInfService.save(borerinf);

        return AjaxResult.success("新建成功");
    }


    /**
     *
     * @param borerinf 所编辑对象
     * @return 编辑结果
     */
    @PutMapping("/update-one")
    public AjaxResult updateOne(@RequestBody BorerInf borerinf){
        //判断钻孔编号不重复
        LambdaQueryWrapper<BorerInf> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(BorerInf::getBorerId,borerinf.getBorerId());
        BorerInf borerinf1 = borerInfService.getOne(queryWrapper);
        //对象不为空并且钻机编号相同
        if(borerinf1!=null&&!borerinf1.getId().equals(borerinf.getId())){
            return AjaxResult.error("该钻机编号已存在");
        }

        LambdaQueryWrapper<BorerInf> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BorerInf::getId, borerinf.getId());

        borerinf.setUpdateTime(MyTimeUtil.nowTime());
        boolean update = borerInfService.update(borerinf, lambdaQueryWrapper);
        if(update){
            return AjaxResult.success("修改成功");
        }else {
            return AjaxResult.error();
        }
    }
}
